from django.contrib import admin
from django.utils.translation import ugettext_lazy as _
from countries_states.models import Country, State


class StateInline(admin.TabularInline):
    model = State
    extra = 1

class CountryAdmin(admin.ModelAdmin):
 
    def make_active(self, request, queryset):
        rows_updated = queryset.update(active=True)
        if rows_updated == 1:
            message_bit = '1 country was'
        else:
            message_bit = '%s countries were' % rows_updated
        self.message_user(request, '%s successfully marked as active' % message_bit)
    make_active.short_description = 'Mark selected countries as active'

    def make_inactive(self, request, queryset):
        rows_updated = queryset.update(active=False)
        if rows_updated == 1:
            message_bit = '1 country was'
        else:
            message_bit = '%s countries were' % rows_updated
        self.message_user(request, '%s successfully marked as inactive' % message_bit)
    make_inactive.short_description = 'Mark selected countries as inactive'


    list_display = ('printable_name', 'iso2_code', 'active')
    list_filter = ('continent', 'active')
    search_fields = ('name', 'iso2_code', 'iso3_code')
    actions = ['make_active', 'make_inactive']
    inlines = [StateInline]

admin.site.register(Country, CountryAdmin)
    
